'''
Created on 2011-4-18

@author: miraclew
'''
from jxgame.globals.GlobalDef import GS_FREE, tagUserStatus, US_NULL,\
    INVALID_CHAIR, INVALID_TABLE, US_SIT, US_LOOKON
from jxgame.globals.GlobalServerice import INVALID_WORD_INDEX
from jxgame.globals.GlobalFrame import MDM_GF_GAME, MDM_GF_FRAME,\
    SUB_GF_USER_READY, CMD_GF_Info, SUB_GF_INFO, SUB_GF_USER_CHAT, SUB_GF_READY,\
    SUB_GF_OPTION, SUB_GF_SCENE
from jxgame.proto.frame_pb2 import CMD_GF_UserChat, CMD_GF_Option
from jxgame.globals.CmdGame import MDM_GR_LOGON, SUB_GR_LOGON_ERROR,\
    SUB_GR_LOGON_FINISH, MDM_GR_USER, SUB_GR_USER_STATUS
from jxgame.proto.room_pb2 import CMD_GR_UserChat, CMD_GR_UserStatus

########################_#######################_#######################
class AIUser:
    #状态变量
    StartClient         = False                 #游戏标志
    GameStatus          = 0                     #游戏状态
    CurrentUserStatus   = None                  #用户状态
    
    #绑定变量
    UserID              = 0                     #用户标识
    ServerUser         = None                  #用户接口
    
    #索引变量
    RoundID             = 0                     #循环索引
    AIIndex             = 0                     #索引变量
    
    #时间组件
    #CTimerItemArray                    m_TimerItemActive;                    //活动数组
    #static CTimerItemArray            m_TimerItemStorage;                    //库存数组

    #接口变量
    ServerUserManager  = 0                     #用户管理
    AIUserManager      = 0                     #管理接口
    AIUserSink         = 0                     #回调接口
    
    def __init__(self):
        self.Reset()
        self.RoundID=1
        self.AIIndex=INVALID_WORD_INDEX
        self.AIUserManager = None
        self.AIUserSink = None
        
    #网络接口
    def SendCMD(self,cmd):
        self.AIUserManager.SendDataToServer(MDM_GF_GAME,cmd)
    
    def SendUserReady(self):
        if self.ServerUser==None or self.ServerUser.GetUserStatus() != US_SIT: return False
        self.AIUserManager.SendDataToServer(MDM_GF_FRAME,SUB_GF_USER_READY)
    
    def SendChatMessage(self,target_user_id, chat_message, font_color):
        cmd = CMD_GF_UserChat()
        cmd.chat_message = chat_message
        cmd.font_color = font_color
        cmd.target_user_id = target_user_id
        cmd.send_user_id = self.ServerUser.user_id
        self.AIUserManager.SendDataToServer(MDM_GF_FRAME, SUB_GF_USER_CHAT,cmd)               
        
    def SetGameTimer(self):
        pass
    
    def OnTimerPulse(self):
        pass
    
    def OnSocketRead(self, main_cmd_id,sub_cmd_id, cmd_data):
        if main_cmd_id==MDM_GR_LOGON and sub_cmd_id==SUB_GR_LOGON_ERROR:
            return self.OnSocketSubLogonFailed(cmd_data)
        if main_cmd_id==MDM_GR_LOGON and sub_cmd_id==SUB_GR_LOGON_FINISH:
            return self.OnSocketSubLogonFinish(cmd_data)
        #用户状态
        if main_cmd_id==MDM_GR_USER and sub_cmd_id==SUB_GR_USER_STATUS:
            return self.OnSocketSubUserStatus(cmd_data)
        if main_cmd_id==MDM_GF_GAME or main_cmd_id==MDM_GF_FRAME:
            return self.OnSocketGameMessage(main_cmd_id,sub_cmd_id,cmd_data)
    
    def OnSocketGameMessage(self,main_cmd_id,sub_cmd_id, cmd_data):
        #游戏处理
        if main_cmd_id==MDM_GF_GAME:
            assert self.AIUserSink != None
            return self.AIUserSink.OnEventGameMessage(sub_cmd_id,cmd_data)        
        #框架处理
        if main_cmd_id==MDM_GF_FRAME:
            if sub_cmd_id==SUB_GF_READY:
                self.SendUserReady(1)
            elif sub_cmd_id==SUB_GF_OPTION:
                cmd = CMD_GF_Option()
                self.GameStatus = cmd.game_status
            elif sub_cmd_id==SUB_GF_SCENE:
                look_on_user = self.ServerUser.user_status==US_LOOKON
                self.AIUserSink.OnEventGameScene(self.GameStatus,look_on_user,cmd_data)
    
    def OnSocketSubLogonFinish(self):
        self.GameStatus = GS_FREE
        self.ServerUser=self.ServerUserManager.SearchOnLineUser(self.UserID)
        assert self.ServerUser!=None
        if self.ServerUser==None: return False
        self.CurrentUserStatus.ChairID=self.ServerUser.chair_id
        self.CurrentUserStatus.TableID=self.ServerUser.table_id
        self.CurrentUserStatus.UserStatus=self.ServerUser.user_status
        #重入判断
        if self.ServerUser.table_id!=INVALID_TABLE:
            self.StartClient()
    
    def OnSocketSubLogonFailed(self):
        self.AIUserManager.DeleteAIUser(self.AIIndex)
    
    def OnSocketSubUserStatus(self,cmd_data):
        cmd = CMD_GR_UserStatus()
        if cmd.user_id!=self.UserID: return True
        self.CurrentUserStatus.ChairID=cmd.chair_id
        self.CurrentUserStatus.TableID=cmd.table_id
        self.CurrentUserStatus.UserStatus=cmd.user_status
        
        if self.StartClient and self.CurrentUserStatus.TableID==INVALID_TABLE:
            self.StopUser()
            return True
        
        if self.StartClient==False and self.CurrentUserStatus.TableID!=INVALID_TABLE:
            self.StartUser()
            return True
        
    #控制函数
    def StartUser(self):
        assert self.StartClient==False  and self.ServerUser!=None
        assert  self.ServerUser.table_id!=INVALID_TABLE and self.ServerUser.chair_id!=INVALID_CHAIR
        self.StartClient = True
        cmd = CMD_GF_Info()
        cmd.AllowLookon =False
        self.AIUserManager.SendDataToServer(MDM_GF_FRAME,SUB_GF_INFO,cmd)
        
    def StopUser(self):
        assert self.StartClient==True  and self.ServerUser!=None
        assert  self.ServerUser.table_id==INVALID_TABLE and self.ServerUser.chair_id==INVALID_CHAIR
        self.KillGameTimer(0)
        self.StartClient = False
        self.GameStatus = GS_FREE
        if self.AIUserSink!=None: self.AIUserSink.Reset()
    
    def Reset(self):
        self.StartClient = False
        self.GameStatus = GS_FREE
        self.CurrentUserStatus = tagUserStatus()
        self.CurrentUserStatus.UserStatus = US_NULL
        self.CurrentUserStatus.ChairID = INVALID_CHAIR
        self.CurrentUserStatus.TableID = INVALID_TABLE
        self.UserID = 0;
        self.ServerUser = None
        
        self.RoundID = max([1,self.RoundID+1])
        if self.AIUserSink!=None: self.AIUserSink.Reset()
        
    def KillGameTimer(self,timer_id):
        pass